import '../../index.dart';

enum SelectUserType {
  all,
  dept,
}

class SelectUserPage extends StatefulWidget {
  late String title;
  late SelectUserType selectUserType;
  late bool isRadio;
  late String? selectUserIds;

  SelectUserPage(
      {Key? key,
      this.selectUserType = SelectUserType.all,
      this.isRadio = true,
      this.selectUserIds,
      this.title = "选择用户"})
      : super(key: key);

  @override
  _SelectUserState createState() => _SelectUserState();
}

class _SelectUserState extends State<SelectUserPage> {
  final PageData<UserInfo> _pageData = PageData<UserInfo>();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if (widget.selectUserType == SelectUserType.all) {
      _pageData.items = UserListCache().getCurrentData() ?? [];
    } else {
      _pageData.items =
          UserListCache().getUserList(Global.profile.user?.deptId ?? "") ?? [];
    }
  }

  @override
  Widget build(BuildContext context) {
    return GlobalStyle(
      child: BackgroundStyleWidget(
        child: Scaffold(
          appBar: AppBar(
            actions: widget.isRadio
                ? null
                : [TextButton(onPressed: () {}, child: Text("确定"))],
            title: Text(
              widget.title,
            ),
            centerTitle: true,
          ),
          body: _buildBody(), // 构建主页面
        ),
      ),
    );
  }

  Widget _buildBody() {
    return _pageData.items.isNotEmpty
        ? ListView.separated(
            itemCount: _pageData.items.length,
            itemBuilder: (context, index) {
              return ListTile(
                title: Text(_pageData.items[index].name ?? ""),
                onTap: () {
                  if (widget.isRadio) {
                    Navigator.pop(context, [_pageData.items[index]]);
                  } else {
                    //修改选择状态
                  }
                },
              );
            },
            separatorBuilder: (BuildContext context, int index) {
              return const Divider(
                height: 1,
              );
            },
          )
        : HintWidget(pageData: _pageData);
  }
}
